/**************************************************************************
	Replication do-file: "Corruption in Customs"
	Cyril Chalendard, Ana Fernandes, Gael Raballand and Bob Rijkers
	
	Created on: 01/07/2022
**************************************************************************/

clear all
set more off, perm
cap log close
cls

* ----------------------- DIRECTORIES AND FOLDERS ----------------------- *

* Directories in which data are stored
global main "PUT YOUR DIRECTORY PATH HERE"
cd "$folder"

* Folders
global outputdata = "$main\Output Data"
cap mkdir "$main\Tables"
global tables = "$main\Tables"

* ----------------------------- BEGINS HERE ----------------------------- *

* -------
* Dataset
* -------
use "$outputdata\CFRR.dta", clear // open dataset

* Sample
keep if sample_int==1 & eis_f!=. // keep observations of interest


* ---------
* Table A23
* ---------

* Globals
global dep "htrl_f_int htrl_f_ext htrl_f_adv"

* Matrix
mat tableA23 = J(12,3,.)
mat tableA23_T = J(1,3,.)

* Loop for estimations
local i = 1
local j = 1
foreach x in $dep {

	qui: reghdfe `x' wfr eis_f wfr_eis_f tax_rate countryorig* max_twodigit* if sample_int==1 & wfr!=. , absorb(ym_*) vce(cluster insp_f key_dec)

	mat tableA23[`i',`j'] = _b[wfr]
	local i = `i' + 1
	mat tableA23[`i',`j'] = _se[wfr]
	local i = `i' + 1
	mat tableA23[`i',`j'] = _b[eis_f]
	local i = `i' + 1
	mat tableA23[`i',`j'] = _se[eis_f]
	local i = `i' + 1
	mat tableA23[`i',`j'] = _b[wfr_eis_f]
	local i = `i' + 1
	mat tableA23[`i',`j'] = _se[wfr_eis_f]	
	mat tableA23_T[1,`j'] = e(N)
	local i = `i' + 6
	mat tableA23[`i',`j'] = e(r2)
	local i = `i' -11
	local j = `j' +1
}


* Export
* ------
putexcel set "$tables\Table A23.xlsx", replace sheet(TableA23) // create a new excel spreadsheet

* Title
putexcel (A1:D1), merge hcenter vcenter
putexcel A1 = "During delegated randomization of inspector assignment"

* Dependent variables
putexcel A2 = "Dependent variable:", left vcenter
putexcel (B2:D2), merge hcenter vcenter bold
putexcel B2 = "Hypothetical revenue loss"

* Reference price
putexcel A3 = "reference price", left vcenter
putexcel B3 = "Importer"
putexcel C3 = "Exporter"
putexcel D3 = "Third-party"

* Coefficients
putexcel B5 = matrix(tableA23), nformat(0.000) hcenter vcenter
putexcel B15 = matrix(tableA23_T), nformat(number_sep) hcenter vcenter

* Independent variables
local j = 5
local labels `" "Excess interaction share" "Withheld from randomization (WFR)" "WFR*Excess interaction share" "'
forvalues i = 1(1)3 {

	local x: word `i' of `labels'

	putexcel A`j' = "`x'", left vcenter
	local j = `j'+2
}
local j = 11
local labels `" "Exogenous declaration characteristics" "Source country fixed effects" "HS2-product fixed effects" "Month-year fixed effects" "Observations" "R-squared" "'
forvalues i = 1(1)6 {

	local x: word `i' of `labels'

	putexcel A`j' = "`x'", left vcenter
	local j = `j'+1
}

* Column numbers
local cells `" "B" "C" "D" "'
forvalues i = 1(1)3 {

	local y: word `i' of `cells'
	putexcel `y'4 = "(`i')", hcenter vcenter
}

* FE
local cells `" "B" "C" "D" "'
forvalues i = 1(1)3 {
	
	local y: word `i' of `cells'
	
	forvalues j = 11(1)14 {
		putexcel `y'`j' = "Yes", hcenter vcenter	
	}
}


* -------------------------------- ENDS HERE -------------------------------- *